package com.wenshi.app.delegate;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.wenshi.app.activity.R;


/**
 * Created by mrren on 2017/3/11.
 * 该类是代理类
 */
public class WsWiewDelegate {

    private static IDelegateListener options;

    public static void setListener(IDelegateListener options) {
        WsWiewDelegate.options = options;
    }

    int base_content_id = 0;
    private boolean _is_show_header = true;
    private boolean _is_showed_header = false;
    private int show_header_id = 0;
    public View root;
    private Context mmcontext;
    private View.OnClickListener top_left_click_linstenr = null;


    public interface IReLoadContent {

        void onReLoadContent();
    }

    public interface IInitData {

        void __onCreate();
    }

    private IReLoadContent iReLoadContent;

    /**
     * 该方法用在父类中
     * 用来传递Context，以及内容视图
     */
    public WsWiewDelegate(Context context, View view) {
        if (options == null) {
            options = new WsDelegateListener();
        }
        mmcontext = context;
        if (context instanceof IReLoadContent) {
            iReLoadContent = (IReLoadContent) context;
        }
        root = view;

        ViewStub loading = (ViewStub) root.findViewById(R.id.base_content_loading_stub);
        options.onShowPageLoading(context, loading, root.findViewById(R.id.base_content_loading));

    }

    /**
     * 是否显示头布局
     * false是不显示，true是显示
     */
    public void setHeaderVisible(boolean show) {
        _is_show_header = show;
    }

    /**
     * 设置自定义头布局
     */
    public void setHeader(int id) {
        show_header_id = id;
    }

    /**
     * 设置内容布局，基本用在父类当中
     */
    public void setContentView(int id) {
        Log.e("init", "setContentView: " + id);
        base_content_id = id;
        if (base_content_id > 0) {
            initHeaderShow();
            ViewStub contentview = (ViewStub) root.findViewById(R.id.base_content);


            contentview.setLayoutResource(base_content_id);
            contentview.setVisibility(View.VISIBLE);

            hideLoadingContent();

        }
    }

    private void initHeaderShow() {
        if (_is_showed_header) {
            return;
        }
        if (_is_show_header) {
            ViewStub headertmp = (ViewStub) root.findViewById(R.id.base_top);


            if (show_header_id > 0) {
                headertmp.setLayoutResource(show_header_id);
            }
            headertmp.setVisibility(View.VISIBLE);


            _is_showed_header = true;
            root.findViewById(R.id.pub_base_top_left).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (top_left_click_linstenr != null) {
                        top_left_click_linstenr.onClick(v);
                    } else {
                        if (iReLoadContent instanceof Activity)
                            ((Activity) iReLoadContent).finish();
                    }

                }
            });

        }
    }


    /**
     * 当数据是空时提供的方法
     */
    public void renderEmptyView() {
        hideLoadingContent();
        ViewStub contentview = (ViewStub) root.findViewById(R.id.base_no_data);

        options.onEmpty(mmcontext, contentview, root);

    }

    /**
     * 当数据加载出错时提供的方法
     */
    public void renderErrorView() {
        hideLoadingContent();
        ViewStub contentview = (ViewStub) root.findViewById(R.id.base_load_error);


        options.onHttpError(mmcontext, contentview, root, iReLoadContent);
    }

    /**
     * 设置头部右边的字体 和点击事件
     * 当点击事件是空时  无任何事件操作
     * 参数 String str,View.OnClickListener l
     */
    public void setHeaderRight(String righttext, View.OnClickListener l) {
        if (!_is_show_header) return;
        initHeaderShow();
        initHeaderRightLayout();
        TextView tv_right = (TextView) root.findViewById(R.id.pub_base_right_text);

        LinearLayout tv_lonear = (LinearLayout) root.findViewById(R.id.pub_base_top_right);

        tv_right.setVisibility(View.VISIBLE);
        tv_lonear.setOnClickListener(l);
        tv_right.setText(righttext);
    }

    public void setHeaderRight(String u_token, String righttext, View.OnClickListener l) {
        if (u_token.equals("")) {

        } else {
            if (!_is_show_header) return;
            initHeaderShow();
            initHeaderRightLayout();
            TextView tv_right = (TextView) root.findViewById(R.id.pub_base_right_text);
            LinearLayout tv_lonear = (LinearLayout) root.findViewById(R.id.pub_base_top_right);
            tv_right.setVisibility(View.VISIBLE);
            tv_lonear.setOnClickListener(l);
            tv_right.setText(righttext);
        }

    }

    /**
     * 设置头布局右边的图片样式以及点击事件
     */
    public void setHeaderRight(int imgid, View.OnClickListener l) {
        if (!_is_show_header) return;
        initHeaderShow();
        initHeaderRightLayout();

        ImageView iv_right = (ImageView) root.findViewById(R.id.pub_base_right_img);

        iv_right.setVisibility(View.VISIBLE);
        LinearLayout tv_lonear = (LinearLayout) root.findViewById(R.id.pub_base_top_right);


        tv_lonear.setOnClickListener(l);
        iv_right.setImageResource(imgid);


    }

    private void initHeaderRightLayout() {
        View tmp = root.findViewById(R.id.base_top_right);
        if (tmp != null) {
            ((ViewStub) tmp).setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置头部左边的布局带图片带字的更改
     */
    public void setHeaderLeft(int imgid, String title, View.OnClickListener l) {
        if (!_is_show_header) return;
        initHeaderShow();
        ImageView iv_left = (ImageView) root.findViewById(R.id.pub_base_left_img);


        if (imgid == -1) {
            iv_left.setVisibility(View.GONE);
        } else if (imgid > 0) {
            iv_left.setImageResource(imgid);
        }
        if (l != null) {
            top_left_click_linstenr = l;
        }


        TextView tv_left = (TextView) root.findViewById(R.id.pub_base_left_text);


        if (!title.equals("") || title == null) {
            tv_left.setText(title);
            tv_left.setVisibility(View.VISIBLE);
        } else {
            tv_left.setVisibility(View.GONE);
        }


    }

    /**
     * 设置头部左边布局的字体
     * 默认是返回事件
     */
    public void setHeaderLeft(String title) {
        setHeaderLeft(0, title, null);
    }

    /**
     * 设置左边头部布局的图片 以及点击事件
     */
    public void setHeaderLeft(int imgid, View.OnClickListener l) {
        setHeaderLeft(imgid, "", l);
    }

    /**
     * 设置头部中间字体
     */
    public void setHeadercenter(String toptitle) {
        if (!_is_show_header) return;
        initHeaderShow();

        TextView tv_center = (TextView) root.findViewById(R.id.pub_base_center_title);
        tv_center.setText(toptitle);
    }

    /**
     * 获取头布局的视图
     */
    public View getHeader() {
        if (!_is_show_header) return null;
        initHeaderShow();
        return root.findViewById(R.id.base_top);
    }

    private void hideLoadingContent() {
        LinearLayout contentview = (LinearLayout) root.findViewById(R.id.base_content_loading);
        options.onHidePageLoading(mmcontext, contentview);

        contentview.setVisibility(View.GONE);
    }

    /**
     * 无Wifi时获取的方法
     */
    public void renderNoWifiView() {
        hideLoadingContent();
        ViewStub contentview = (ViewStub) root.findViewById(R.id.base_no_wifi);

        options.onNoWifi(mmcontext, contentview, root);


    }

}
